package com.hibernate.utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateFactory {
	private static SessionFactory sf;
	public static SessionFactory getSf() {
		return sf;
	}

public static SessionFactory buildSessionFactory()
{
	Configuration cfg = new Configuration();
	cfg.configure();
	sf = cfg.buildSessionFactory();
	return sf;
}
public static Session openSession()throws HibernateException
{
	return sf.openSession();
	}
public static void closeFactory()
{
	if(sf!=null)
		try {
			sf.close();
		} catch (HibernateException ignored) {
			// ignore
			ignored.printStackTrace();
		}
	}

public static void close(Session s)
{
	if(s!=null)
	try{	s.close();
	}catch (HibernateException ignored) {
		ignored.printStackTrace();
	}
	}
public static void rollback(Transaction tx)
{
	try {
		if(tx!=null)
			tx.rollback();
	} catch (HibernateException e) {
		e.printStackTrace();
	}
	}

}
